package com.xh.bussiness.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xh.core.sms.dto.http.HttpClient;
import com.xh.core.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.jingyou.service.IJYBaseDataService;
import com.xh.bussiness.system.bean.dto.AreaDto;
import com.xh.bussiness.system.bean.request.AreaRequest;
import com.xh.bussiness.system.dao.AreaMapper;
import com.xh.bussiness.system.model.Area;
import com.xh.bussiness.system.service.IAreaService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.util.CollectionUtils;

@Service
public class AreaServiceImpl extends BaseServiceImpl<AreaMapper, Area> implements IAreaService {

	@Override
	public List<Area> queryAll(){
		return super.queryAll();
	}

	@Override
	public List<AreaDto> queryTree(AreaRequest bean) {
		QueryWrapper w = new QueryWrapper();
		List<AreaDto> all = super.selectList(w , AreaDto.class);
		List<AreaDto> roots = getRoots(all);
		getTree(roots, all);
		return roots;
	}

	@Override
	public List<Area> queryByParentId(String parentId) {
		QueryWrapper<Area> qw = new QueryWrapper<>();
		if (StringUtils.isEmpty(parentId)) {
			qw.isNull("parent_id");
		}else{
			qw.eq("parent_id", parentId);
		}
		List<Area> areas = this.dao.selectList(qw);
		if (!CollectionUtils.isEmpty(areas)) {
			for (Area area : areas) {
				Integer childrenCount = queryCountByParentId(area.getId());
				if (childrenCount > 0) {
					area.setLeaf(false);
				}
			}
		}
		return areas;
	}

	public Integer queryCountByParentId(String parentId) {
		QueryWrapper<Area> qw = new QueryWrapper<>();
		qw.eq("parent_id", parentId);
		return this.dao.selectCount(qw);
	}

	private List<AreaDto> getRoots(List<AreaDto> all){
		List<AreaDto> roots = new ArrayList<AreaDto>();
		all.forEach( area -> {
			if(StringUtils.isEmpty(area.getParentId())){
				roots.add(area);
			}
		});
		return roots;
	}
	
	private void getTree(List<AreaDto> roots, List<AreaDto> all){
		for(AreaDto root : roots){
			List<AreaDto> child = new ArrayList<AreaDto>();
			for(AreaDto area : all){
				if(area.getParentId().equals(root.getId())){
					child.add(area);
				}
			}
			root.setChild(child);
			getTree(child, all);
		}
	}

	List<Area> result = new ArrayList<>();

	@Override
	public List<Area> initArea() {
		List<Area> all = null;

		String request = HttpUtils.getRequest("https://www.aixuebanban.com/v2/global/cities", new HashMap<>());

		JSONObject jsonObject = JSONObject.parseObject(request);

		List<JSONObject> datas = (List<JSONObject>)jsonObject.get("data");

		for (JSONObject data : datas) {
			Area area = new Area();
			area.setId(data.get("id").toString());
			area.setName(data.get("name").toString());
			area.setParentId(null);

			result.add(area);
			getChildren((List<JSONObject>) data.get("children"), data);

		}

		this.dao.truncateTable();
		this.dao.batchInsert(result);
		return all;
	}

	private void getChildren(List<JSONObject> childs,JSONObject parent) {
		for (JSONObject child : childs) {
			Area area = new Area();
			area.setId(child.get("id").toString());
			area.setName(child.get("name").toString());
			area.setParentId(parent.get("id").toString());
			if (!CollectionUtils.isEmpty((JSONArray)child.get("children"))) {
				getChildren((List<JSONObject>)child.get("children"), child);
			}
			result.add(area);
		}
	}
}
